package com.iweb;

import com.iweb.config.AppConfig;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletRegistration;

public class HelloWorldApplicationInitializer implements WebApplicationInitializer {
    @Override
    public void onStartup(ServletContext context) {

        /*
         *
         <init-param>
            <!--springmvc的核心配置文件的路径-->
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:mvc.xml</param-value>
         </init-param>
         * 但是这里用的不是xml 是java
         * */
        AnnotationConfigWebApplicationContext ac = new AnnotationConfigWebApplicationContext();
        ac.register(AppConfig.class);
        ac.refresh();

        /*<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>*/
        DispatcherServlet servlet = new DispatcherServlet(ac);
        /*<servlet-name>helloWorld</servlet-name>*/
        ServletRegistration.Dynamic registration = context.addServlet("app", servlet);
        /*<load-on-startup>1</load-on-startup>*/
        registration.setLoadOnStartup(1);
        /*<url-pattern>/</url-pattern>*/
        registration.addMapping("/");

    }
}
